package scau.hotel.hotel_web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import scau.hotel.hotel_service.entity.RoomType;
import scau.hotel.hotel_service.service.RoomTypeService;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_service.vo.RoomTypeVo;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (RoomType)表控制层
 *
 * @author makejava
 * @since 2022-03-03 23:22:52
 */
@RestController
@PreAuthorize("hasAnyAuthority('admin','normal')")
@RequestMapping("roomType")
@Api(value = "typeApi", tags = "房间类别控制器")
public class RoomTypeController {
    /**
     * 服务对象
     */
    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 不分页查询
     *
     * @param roomType 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("不分页查询")
    public RespBean queryAll(RoomType roomType) {
        return RespBean.success(this.roomTypeService.queryAll(roomType));
    }

    /**
     * 分页查询
     *
     * @param pageNum  起始页
     * @param pageSize 每页数据大小
     * @param roomType     筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public RespBean page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, RoomType roomType) {
        PageHelper.startPage(pageNum, pageSize);
        List<RoomType> list = roomTypeService.queryAll(roomType);
        PageInfo<List> pageInfo = new PageInfo(list);
        return RespBean.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param roomTypeId 主键
     * @return 单条数据
     */
    @GetMapping("{roomTypeId}")
    @ApiOperation("通过房型id查询单条数据")
    public RespBean queryById(@PathVariable("roomTypeId") Integer roomTypeId) {
        return RespBean.success(this.roomTypeService.queryById(roomTypeId));
    }

    /**
     * 新增数据
     *
     * @param roomTypeVo 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增房型数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean add(@RequestBody @Valid RoomTypeVo roomTypeVo) {
        return this.roomTypeService.insert(roomTypeVo);
    }

    /**
     * 编辑数据
     *
     * @param roomTypeVo 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑房型数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean edit(@RequestBody @Valid RoomTypeVo roomTypeVo) {
        return this.roomTypeService.update(roomTypeVo);
    }

    /**
     * 删除数据
     *
     * @param roomTypeId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{roomTypeId}")
    @ApiOperation("删除房型数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean deleteById(@PathVariable Integer roomTypeId) {
        return RespBean.success(this.roomTypeService.deleteById(roomTypeId));
    }

}

